package com.ll.domain.classbase.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.base.util.ScalarUtil;
import com.ll.dal.mapper.ClassBaseMapper;
import com.ll.dal.po.ClassBasePO;
import com.ll.domain.classbase.base.ClassBaseService;
import com.ll.domain.classbase.enums.ClassBaseStatusEnum;
import com.ll.base.dto.PageReqDTO;
import com.ll.base.dto.PageResultDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ClassBaseServiceImpl extends ServiceImpl<ClassBaseMapper, ClassBasePO> implements ClassBaseService {

    private final ClassBaseMapper classBaseMapper;

    @Override
    public PageResultDTO<ClassBasePO> page(PageReqDTO req) {
        req.initPage();
        IPage iPage = new Page(req.getPageNo(), req.getPageSize());
        iPage = classBaseMapper.selectPage(iPage, new LambdaQueryWrapper<ClassBasePO>()
                .orderByDesc(ClassBasePO::getIndexNum)
        );
        PageResultDTO PageResultDTO = new PageResultDTO<>(iPage.getRecords(), iPage.getTotal(), req);
        return PageResultDTO;
    }

    @Override
    public List<ClassBasePO> listByIndexNums(List<Integer> indexIds) {
        return classBaseMapper.selectList(new LambdaQueryWrapper<ClassBasePO>().in(ClassBasePO::getIndexNum, indexIds));
    }

    @Override
    public void updateExtData(Long id, String data) {
        ClassBasePO classBasePO = new ClassBasePO();
        classBasePO.setId(id);
        classBasePO.setExtData(data);
        classBaseMapper.updateById(classBasePO);
    }

    @Override
    public void updateStatus(Long id, ClassBaseStatusEnum status) {
        ClassBasePO classBasePO = new ClassBasePO();
        classBasePO.setId(id);
        classBasePO.setStatus(status);
        classBaseMapper.updateById(classBasePO);
    }

    @Override
    public boolean checkStudent(String dingUid) {
        List<ClassBasePO> classBasePOS = classBaseMapper.selectList(new LambdaQueryWrapper<ClassBasePO>()
                .like(ClassBasePO::getExtData, dingUid + ","));

        if(ScalarUtil.isEmpty(classBasePOS)){
            return false;
        }

        for(ClassBasePO po : classBasePOS){
            if(po.getStatus() == ClassBaseStatusEnum.开班中){
                return true;
            }
        }
        return false;
    }

}
